package vn.lumi.smarthome.ui;

import vn.lumi.smarthome.ActivityHelper;
import vn.lumi.smarthome.LumiHome;
import vn.lumi.smarthome.R;
import vn.lumi.smarthome.io.database.TBL_Schedule;
import vn.lumi.smarthome.io.model.Device;
import vn.lumi.smarthome.io.model.Schedule;
import vn.lumi.smarthome.io.model.Schedule.OnControlScheduleListener;
import vn.lumi.smarthome.ui.DeviceFragment.DeviceReceiver;
import vn.lumi.smarthome.ui.EditScheduleActivity.DeleteScheduleReceiver;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;

public class ScheduleListActivity extends SherlockFragmentActivity implements
		LoaderCallbacks<Cursor>, OnControlScheduleListener, OnClickListener {

	private static final String TAG = "ScheduleListActivity";
	private ListView mListSchedules;

	private ScheduleAdapter mScheduleAdapter;

	private int deviceId = 0;
	private int roomId = 0;
	private String deviceName = "";
	private int deviceType;

	LinearLayout layoutActionbar;
	Button btn_edit;
	Button btn_add;
	TextViewCustom tv_title;

	public static boolean isEdit = false;

	// Khai bao ID cua Loader
	private static final int LOADER_ID = 10;

	public static final String EXTRA_DEVICE_ID = "device_id";
	public static final String EXTRA_DEVICE_NAME = "device_name";
	public static final String EXTRA_DEVICE_TYPE = "device type";
	public static final String EXTRA_ROOM_ID = "room_id";

	public static final String DEVICE_ID = "device id";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_schedule_list_fragment);
		deviceId = getIntent().getIntExtra(EXTRA_DEVICE_ID, -1);
		deviceName = getIntent().getStringExtra(EXTRA_DEVICE_NAME);
		deviceType = getIntent().getIntExtra(EXTRA_DEVICE_TYPE, -1);
		roomId = getIntent().getIntExtra(EXTRA_ROOM_ID, -1);
		setupActionBar();
		init_view();
		init_data();
		init_loader();
		ActivityHelper.initialize(this);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.actionbar_schedule_list_add:
			if (mScheduleAdapter.getCount() < 3) {
				Intent i = new Intent(this, AddScheduleActivity.class);
				i.putExtra(AddScheduleActivity.EXTRA_DEVICE_ID, deviceId);
				i.putExtra(AddScheduleActivity.EXTRA_DEVICE_NAME, deviceName);
				i.putExtra(AddScheduleActivity.EXTRA_DEVICE_TYPE, deviceType);
				startActivity(i);
			} else {
				showCustomDialog(R.string.txt_error, R.string.txt_max_schedules);
			}
			break;

		case R.id.actionbar_schedule_list_edit:
			if (isEdit) {
				btn_edit.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.img_editscene_selector));
				isEdit = false;
				// refresh lisview
				restart_loader();
			} else {
				btn_edit.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.img_ok));
				isEdit = true;
				// refresh lisview
				restart_loader();
			}
			break;
		default:
			break;
		}
	}

	public static void setMode(boolean mode) {
		isEdit = mode;
	}

	/**
	 * @return: true: edit
	 * 
	 */
	public static boolean getMode() {
		return isEdit;
	}

	// -----------------------------Actionbar------------------
	private void setupActionBar() {
		ActionBar actionbar = getSupportActionBar();
		actionbar.setCustomView(R.layout.actionbar_schedule_list);
		actionbar.setDisplayShowHomeEnabled(false);
		actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
				| ActionBar.DISPLAY_SHOW_HOME);
		actionbar.setDisplayShowHomeEnabled(false);
		actionbar.setDisplayShowTitleEnabled(false);
		layoutActionbar = (LinearLayout) actionbar.getCustomView();
		tv_title = (TextViewCustom) findViewById(R.id.actionbar_schedule_list_title);
		tv_title.setText(getString(R.string.txt_schedule_device) + deviceName);
		btn_add = (Button) findViewById(R.id.actionbar_schedule_list_add);
		btn_add.setOnClickListener(this);
		btn_edit = (Button) findViewById(R.id.actionbar_schedule_list_edit);
		btn_edit.setOnClickListener(this);
		setMode(false);

	}

	private void init_view() {
		mListSchedules = (ListView) findViewById(R.id.activity_schedule_list_schedule);

		mScheduleAdapter = new ScheduleAdapter(this, null,
				CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
		mScheduleAdapter.initData(deviceId, deviceName);
		mScheduleAdapter.setControlSchedule(this);
		mListSchedules.setAdapter(mScheduleAdapter);
	}

	private void init_data() {

	}
	@Override
	public void finish() {
		// TODO Auto-generated method stub
		Intent intentMainActivity = new Intent();
		intentMainActivity.putExtra(MainActivity.STRING_ANOTHER_REQUEST, true);
		intentMainActivity.putExtra("RoomID", roomId);
		setResult(RESULT_OK, intentMainActivity);
		super.finish();
	}

	private void init_loader() {
		Bundle bundle = new Bundle();
		bundle.putInt(DEVICE_ID, deviceId);
		getSupportLoaderManager().initLoader(LOADER_ID, bundle, this);
	}

	private void restart_loader() {
		Bundle bundle = new Bundle();
		bundle.putInt(DEVICE_ID, deviceId);
		getSupportLoaderManager().restartLoader(LOADER_ID, bundle, this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.LoaderManager.LoaderCallbacks#onCreateLoader(int,
	 * android.os.Bundle)
	 */
	@Override
	public Loader<Cursor> onCreateLoader(int id, Bundle bundle) {
		// TODO Auto-generated method stub
		int device_id = bundle.getInt(DEVICE_ID);
		String selection = TBL_Schedule.DEVICE_ID + "=?";
		String[] selectionArgs = new String[] { device_id + "" };
		return new CursorLoader(getApplicationContext(),
				TBL_Schedule.CONTENT_URI_SCHEDULE, null, selection,
				selectionArgs, null);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.app.LoaderManager.LoaderCallbacks#onLoadFinished(android.content
	 * .Loader, java.lang.Object)
	 */
	@Override
	public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
		// TODO Auto-generated method stub
		mScheduleAdapter.swapCursor(arg1);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.app.LoaderManager.LoaderCallbacks#onLoaderReset(android.content
	 * .Loader)
	 */
	@Override
	public void onLoaderReset(Loader<Cursor> arg0) {
		// TODO Auto-generated method stub
		mScheduleAdapter.swapCursor(null);
	}

	private class ScheduleAdapter extends CursorAdapter {
		int deviceId = 0;
		String deviceName = "";
		private static final String TAG = "SceneAdapter";

		private OnControlScheduleListener mOnControlScheduleListener;

		public ScheduleAdapter(Context context, Cursor c, int flags) {
			super(context, c, flags);
		}

		public void initData(int deviceId, String name) {
			this.deviceId = deviceId;
			this.deviceName = name;
		}

		public void setControlSchedule(
				OnControlScheduleListener onControlScheduleListener) {
			this.mOnControlScheduleListener = onControlScheduleListener;
		}

		@Override
		public void bindView(View view, final Context context, Cursor cursor) {
			LinearLayout scheduleDescription = (LinearLayout) view
					.findViewById(R.id.item_listview_schedule_name);
			LinearLayout linear_schedule_on = (LinearLayout) view
					.findViewById(R.id.item_listview_schedule_linear_schedule_on);
			LinearLayout linear_schedule_off = (LinearLayout) view
					.findViewById(R.id.item_listview_schedule_linear_schedule_off);
			
			TextViewCustom img_tv_schedule_on = (TextViewCustom) view
					.findViewById(R.id.item_listview_schedule_img_tv_on);
			TextViewCustom tv_schedule_on = (TextViewCustom) view
					.findViewById(R.id.item_listview_schedule_on);
			
			TextViewCustom tv_schedule_off = (TextViewCustom) view
					.findViewById(R.id.item_listview_schedule_off);
			TextViewCustom img_tv_schedule_off = (TextViewCustom) view
					.findViewById(R.id.item_listview_schedule_img_tv_off);

			ImageView img_icon = (ImageView) view
					.findViewById(R.id.item_listview_schedule_name_icon);
			ImageView img_t2 = (ImageView) view
					.findViewById(R.id.item_listview_schedule_name_t2);
			ImageView img_t3 = (ImageView) view
					.findViewById(R.id.item_listview_schedule_name_t3);
			ImageView img_t4 = (ImageView) view
					.findViewById(R.id.item_listview_schedule_name_t4);
			ImageView img_t5 = (ImageView) view
					.findViewById(R.id.item_listview_schedule_name_t5);
			ImageView img_t6 = (ImageView) view
					.findViewById(R.id.item_listview_schedule_name_t6);
			ImageView img_t7 = (ImageView) view
					.findViewById(R.id.item_listview_schedule_name_t7);
			ImageView img_cn = (ImageView) view
					.findViewById(R.id.item_listview_schedule_name_cn);

			ImageView imgState = (ImageView) view
					.findViewById(R.id.item_listview_schedule_check);

			// // set name
			String timeOn = "";
			String timeOff = "";
			if (cursor.getInt(cursor.getColumnIndex(TBL_Schedule.HOUR)) > 24)
				// description += "\n" + "Giờ bật:Không bật";
				timeOn = "";
			else
				timeOn = timeOn
						+ AbstractSceneActivity.padding_str(cursor
								.getInt(cursor
										.getColumnIndex(TBL_Schedule.HOUR)))
						+ ":"
						+ AbstractSceneActivity.padding_str(cursor
								.getInt(cursor
										.getColumnIndex(TBL_Schedule.MINUTE)));

			if (cursor.getInt(cursor.getColumnIndex(TBL_Schedule.HOUR_END)) == Schedule.SCHEDULE_NOT_TURNOFF_TYPE)
				// description += "\n" + "Giờ tắt:Không tắt" + "\n";
				timeOff = "";
			else
				// description += "\n"
				// + "Giờ tắt: "
				timeOff = timeOff
						+ AbstractSceneActivity
								.padding_str(cursor.getInt(cursor
										.getColumnIndex(TBL_Schedule.HOUR_END)))
						+ ":"
						+ AbstractSceneActivity
								.padding_str(cursor.getInt(cursor
										.getColumnIndex(TBL_Schedule.MINUTE_END)))
						+ "\n";

			tv_schedule_on.setText(timeOn);
			tv_schedule_off.setText(timeOff);

			int repeat = cursor.getInt(cursor
					.getColumnIndex(TBL_Schedule.REPEAT));
			Log.e("repeat", "" + repeat);
			if (repeat == Schedule.SCHEDULE_REPEAT_ON) {
				img_icon.setImageResource(R.drawable.img_schedule_name_on);
				if (cursor.getInt((cursor.getColumnIndex(TBL_Schedule.MONDAY))) == Schedule.SCHEDULE_DAY_ON)
					img_t2.setImageResource(R.drawable.img_schedule_name_t2);
				else
					img_t2.setImageResource(R.drawable.img_schedule_name_off_t2);
				if (cursor
						.getInt((cursor.getColumnIndex(TBL_Schedule.TUESDAY))) == Schedule.SCHEDULE_DAY_ON)
					img_t3.setImageResource(R.drawable.img_schedule_name_t3);
				else
					img_t3.setImageResource(R.drawable.img_schedule_name_off_t3);
				if (cursor.getInt((cursor
						.getColumnIndex(TBL_Schedule.WEDNESDAY))) == Schedule.SCHEDULE_DAY_ON)
					img_t4.setImageResource(R.drawable.img_schedule_name_t4);
				else
					img_t4.setImageResource(R.drawable.img_schedule_name_off_t4);
				if (cursor
						.getInt((cursor.getColumnIndex(TBL_Schedule.THURSDAY))) == Schedule.SCHEDULE_DAY_ON)
					img_t5.setImageResource(R.drawable.img_schedule_name_t5);
				else
					img_t5.setImageResource(R.drawable.img_schedule_name_off_t5);
				if (cursor.getInt((cursor.getColumnIndex(TBL_Schedule.FRIDAY))) == Schedule.SCHEDULE_DAY_ON)
					img_t6.setImageResource(R.drawable.img_schedule_name_t6);
				else
					img_t6.setImageResource(R.drawable.img_schedule_name_off_t6);
				if (cursor
						.getInt((cursor.getColumnIndex(TBL_Schedule.SATURDAY))) == Schedule.SCHEDULE_DAY_ON)
					img_t7.setImageResource(R.drawable.img_schedule_name_t7);
				else
					img_t7.setImageResource(R.drawable.img_schedule_name_off_t7);
				if (cursor.getInt((cursor.getColumnIndex(TBL_Schedule.SUNDAY))) == Schedule.SCHEDULE_DAY_ON)
					img_cn.setImageResource(R.drawable.img_schedule_name_cn);
				else
					img_cn.setImageResource(R.drawable.img_schedule_name_off_cn);
			} else {
				img_icon.setImageResource(R.drawable.img_schedule_name_off);
				img_t2.setImageResource(R.drawable.img_schedule_name_off_t2);
				img_t3.setImageResource(R.drawable.img_schedule_name_off_t3);
				img_t4.setImageResource(R.drawable.img_schedule_name_off_t4);
				img_t5.setImageResource(R.drawable.img_schedule_name_off_t5);
				img_t6.setImageResource(R.drawable.img_schedule_name_off_t6);
				img_t7.setImageResource(R.drawable.img_schedule_name_off_t7);
				img_cn.setImageResource(R.drawable.img_schedule_name_off_cn);
			}
			// set state image
			final int state = cursor.getInt(cursor
					.getColumnIndex(TBL_Schedule.STATE));
			if (!getMode()) {
				
					if (state == Schedule.SCHEDULE_STATE_OFF) {
						imgState.setImageResource(R.drawable.img_tickoff);
					} else if (state == Schedule.SCHEDULE_STATE_ON) {
						imgState.setImageResource(R.drawable.img_tickon);
					}
				
			} else {
				// icon xoa lich
				imgState.setImageResource(R.drawable.img_scene_edit_delete);
			}
			
			if(deviceType == Device.DEVICE_TYPE_HOR_BLIND){
				img_tv_schedule_on.setBackgroundResource(R.drawable.ic_blind_8);
				img_tv_schedule_on.setText("");
				
				img_tv_schedule_off.setBackgroundResource(R.drawable.ic_blind_1);
				img_tv_schedule_off.setText("");
			}
			else if (deviceType == Device.DEVICE_TYPE_RGB) {
				img_tv_schedule_on.setText("");
				img_tv_schedule_on.setBackgroundResource(R.drawable.img_rgb_white);
				
				img_tv_schedule_off.setText("");
				img_tv_schedule_off.setBackgroundResource(R.drawable.img_rgb_black);
			}

			// state image onclick
			final int scheduleId = cursor.getInt(cursor
					.getColumnIndex(TBL_Schedule._ID));
			imgState.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (!getMode()) {
						if (state == Schedule.SCHEDULE_STATE_OFF) {
							// turn on schedule
							mOnControlScheduleListener
									.OnTurnOnSchedule(scheduleId);
							Log.d(TAG, "Turn on scheduleID = " + scheduleId);
						} else if (state == Schedule.SCHEDULE_STATE_ON) {
							// turn off schedule
							mOnControlScheduleListener
									.OnTurnOffSchedule(scheduleId);
							Log.d("SceneFragment", "Turn off scheduleID = "
									+ scheduleId);
						}
					} else {
						// // Xoa lich
						deleteSchedule(mContext, scheduleId, deviceId);

					}
				}

			});

			// config onclick --> edit schedule activity
			scheduleDescription.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					if (getMode()) {
						Intent intent = new Intent(context,
								EditScheduleActivity.class);
						intent.putExtra(EditScheduleActivity.EXTRA_SCHEDULE_ID,
								scheduleId);
						intent.putExtra(EditScheduleActivity.EXTRA_DEVICE_NAME,
								deviceName);
						intent.putExtra(EditScheduleActivity.EXTRA_DEVICE_ID,
								deviceId);
						intent.putExtra(EditScheduleActivity.EXTRA_DEVICE_TYPE,
								deviceType);
						context.startActivity(intent);
					}
				}

			});
			linear_schedule_on.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if (getMode()) {
						Intent intent = new Intent(context,
								EditScheduleActivity.class);
						intent.putExtra(EditScheduleActivity.EXTRA_SCHEDULE_ID,
								scheduleId);
						intent.putExtra(EditScheduleActivity.EXTRA_DEVICE_NAME,
								deviceName);
						intent.putExtra(EditScheduleActivity.EXTRA_DEVICE_ID,
								deviceId);
						context.startActivity(intent);
					}
				}
			});
			linear_schedule_off.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if (getMode()) {
						Intent intent = new Intent(context,
								EditScheduleActivity.class);
						intent.putExtra(EditScheduleActivity.EXTRA_SCHEDULE_ID,
								scheduleId);
						intent.putExtra(EditScheduleActivity.EXTRA_DEVICE_NAME,
								deviceName);
						intent.putExtra(EditScheduleActivity.EXTRA_DEVICE_ID,
								deviceId);
						context.startActivity(intent);
					}
				}
			});

		}

		@Override
		public View newView(Context context, Cursor cursor, ViewGroup viewgroup) {
			return View.inflate(context, R.layout.item_listview_listschedule,
					null);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see vn.lumi.smarthome.io.model.Schedule.OnControlScheduleListener#
	 * OnTurnOnSchedule(int)
	 */
	@Override
	public void OnTurnOnSchedule(int scheduleId) {
		// TODO Auto-generated method stub
		ContentValues values = new ContentValues();
		values.put("scheduleId", scheduleId);
		getApplicationContext().getContentResolver().update(
				TBL_Schedule.CONTENT_URI_SCHEDULE_TURN_ON, values, null, null);

		// show dialog
		ProgressDialog dialog = new ProgressDialog(new ContextThemeWrapper(
				this, R.style.AlertDialogCustom));
		dialog.setMessage(getString(R.string.txt_cotroling_device_in_HC));
		dialog.setCancelable(false);
		dialog.show();
		// Thay doi font chu cua Dialog
		ChangeFont changeFont = new ChangeFont(getApplicationContext());
		TextView textView = (TextView) dialog
				.findViewById(android.R.id.message);
		textView = changeFont.changeFontTextView(textView,
				R.string.txt_cotroling_device_in_HC, LumiHome.font, 16,
				Gravity.NO_GRAVITY,
				getResources().getColor(R.color.greenActionbar));

		// set broadcast receiver
		ToogleScheduleReceiver mReceiver = new ToogleScheduleReceiver(dialog);
		IntentFilter filter = new IntentFilter();
		filter.addAction(this
				.getString(R.string.txt_broadcast_Toogle_schedule_successful));
		filter.addAction(this
				.getString(R.string.txt_broadcast_Toogle_schedule_failed));
		this.registerReceiver(mReceiver, filter);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see vn.lumi.smarthome.io.model.Schedule.OnControlScheduleListener#
	 * OnTurnOffSchedule(int)
	 */
	@Override
	public void OnTurnOffSchedule(int scheduleId) {
		// TODO Auto-generated method stub
		ContentValues values = new ContentValues();
		values.put("scheduleId", scheduleId);
		getApplicationContext().getContentResolver().update(
				TBL_Schedule.CONTENT_URI_SCHEDULE_TURN_OFF, values, null, null);

		// show dialog
		ProgressDialog dialog = new ProgressDialog(new ContextThemeWrapper(
				this, R.style.AlertDialogCustom));
		dialog.setMessage(getString(R.string.txt_cotroling_device_in_HC));
		dialog.setCancelable(false);
		dialog.show();
		// Thay doi font chu cua Dialog
		ChangeFont changeFont = new ChangeFont(getApplicationContext());
		TextView textView = (TextView) dialog
				.findViewById(android.R.id.message);
		textView = changeFont.changeFontTextView(textView,
				R.string.txt_cotroling_device_in_HC, LumiHome.font, 16,
				Gravity.NO_GRAVITY,
				getResources().getColor(R.color.greenActionbar));

		// set broadcast receiver
		ToogleScheduleReceiver mReceiver = new ToogleScheduleReceiver(dialog);
		IntentFilter filter = new IntentFilter();
		filter.addAction(this
				.getString(R.string.txt_broadcast_Toogle_schedule_successful));
		filter.addAction(this
				.getString(R.string.txt_broadcast_Toogle_schedule_failed));
		this.registerReceiver(mReceiver, filter);
	}

	public void deleteSchedule(Context mContext, int scheduleId, int deviceId) {
		ContentValues values = new ContentValues();
		values.put("Schedule id", scheduleId);
		values.put("device_id", deviceId);
		mContext.getContentResolver().update(
				TBL_Schedule.CONTENT_URI_SCHEDULE_DELETE, values, null, null);

		// show dialog
		ProgressDialog dialog = new ProgressDialog(new ContextThemeWrapper(
				mContext, R.style.AlertDialogCustom));
		dialog.setMessage(mContext
				.getString(R.string.txt_deleting_schedule_in_HC));
		dialog.setCancelable(false);
		dialog.show();
		// Thay doi font chu cua Dialog
		ChangeFont changeFont = new ChangeFont(mContext);
		TextView textView = (TextView) dialog
				.findViewById(android.R.id.message);
		textView = changeFont.changeFontTextView(textView,
				R.string.txt_deleting_schedule_in_HC, LumiHome.font, 16,
				Gravity.NO_GRAVITY,
				mContext.getResources().getColor(R.color.greenActionbar));

		// set broadcast receiver
		DeleteScheduleReceiver mReceiver = new DeleteScheduleReceiver(dialog);
		IntentFilter filter = new IntentFilter();
		filter.addAction(mContext
				.getString(R.string.txt_broadcast_del_schedule_sucessful));
		filter.addAction(mContext
				.getString(R.string.txt_broadcast_del_schedule_failed));
		mContext.registerReceiver(mReceiver, filter);
	}

	public class ToogleScheduleReceiver extends BroadcastReceiver {

		private ProgressDialog mDialog;

		public ToogleScheduleReceiver(ProgressDialog dialog) {
			mDialog = dialog;
		}

		@Override
		public void onReceive(Context context, Intent intent) {
			mDialog.cancel();
			unregisterReceiver(this);

			if (intent
					.getAction()
					.equals(context
							.getString(R.string.txt_broadcast_Toogle_schedule_successful))) {

			}

			else if (intent
					.getAction()
					.equals(context
							.getString(R.string.txt_broadcast_Toogle_schedule_failed)))
				// displaySendFailed();
				showCustomDialog(R.string.txt_error,
						R.string.txt_error_cannot_connect_HC);

		}

	}

	public class DeleteScheduleReceiver extends BroadcastReceiver {

		private ProgressDialog mDialog;

		public DeleteScheduleReceiver(ProgressDialog dialog) {
			mDialog = dialog;
		}

		@Override
		public void onReceive(Context context, Intent intent) {
			mDialog.cancel();
			unregisterReceiver(this);
			if (intent
					.getAction()
					.equals(context
							.getString(R.string.txt_broadcast_del_schedule_sucessful))) {

			}

			else if (intent
					.getAction()
					.equals(context
							.getString(R.string.txt_broadcast_del_schedule_failed)))
				// displayEditScheduleFailed();
				showCustomDialog(R.string.txt_error,
						R.string.txt_error_cannot_connect_HC);

		}

	}

	public void showCustomDialog(int title, int message) {
		AlertDialog.Builder ErrorDialog = new AlertDialog.Builder(
				new ContextThemeWrapper(this, R.style.AlertDialogCustom));

		ChangeFont changeFont = new ChangeFont(getApplicationContext());
		TextView tvTitle = new TextView(getApplicationContext());
		tvTitle = changeFont.changeFontTextView(tvTitle, title,
				LumiHome.fontBold, LumiHome.sizeTittle, Gravity.CENTER,
				getResources().getColor(R.color.green));
		ErrorDialog.setCustomTitle(tvTitle);
		ErrorDialog.setMessage(message);
		ErrorDialog.setNegativeButton(R.string.glb_txt_return,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				});
		AlertDialog alertErrorDialog = ErrorDialog.create();
		alertErrorDialog.show();
		alertErrorDialog.getWindow().getAttributes();
		TextView tvMessage = (TextView) alertErrorDialog
				.findViewById(android.R.id.message);
		tvMessage = changeFont.changeFontTextView(tvMessage, message,
				LumiHome.font, LumiHome.sizeContent, Gravity.NO_GRAVITY,
				getResources().getColor(R.color.greenLight));

		Button btn1 = alertErrorDialog
				.getButton(DialogInterface.BUTTON_NEGATIVE);
		btn1.setTypeface(Typeface.createFromAsset(getAssets(),
				LumiHome.fontBold));
		btn1.setTextColor(getResources().getColor(R.color.greenActionbar));
		btn1.setTextSize(LumiHome.sizeButton);
	}
}
